package Streamer;


import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Window {

	private JFrame frame;
	private JTextField textIp;
	private JTextField textPort;
	private JLabel lblPortNumber;
	private JComboBox comboConnection;
	private JProgressBar progressBar;
	private JButton button;
	private Controller controller;
	

	public Window(Controller controller) 
	{
		this.controller = controller;
		initialize();
	}
	
	public void setVisible(boolean visible)
	{
		frame.setVisible(visible);
	}

	
	private void initialize() 
	{
		frame = new JFrame();
		frame.setBounds(100, 100, 236, 318);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		textIp = new JTextField();
		textIp.setText("127.0.0.1");
		textIp.setFont(new Font("Tahoma", Font.PLAIN, 22));
		textIp.setBounds(10, 26, 200, 40);
		frame.getContentPane().add(textIp);
		textIp.setColumns(10);
		
		JLabel lblServerIp = new JLabel("Server IP");
		lblServerIp.setBounds(10, 11, 200, 14);
		frame.getContentPane().add(lblServerIp);
		
		textPort = new JTextField();
		textPort.setText("9000");
		textPort.setFont(new Font("Tahoma", Font.PLAIN, 22));
		textPort.setColumns(10);
		textPort.setBounds(10, 95, 86, 40);
		frame.getContentPane().add(textPort);
		
		lblPortNumber = new JLabel("Port number");
		lblPortNumber.setBounds(10, 77, 86, 14);
		frame.getContentPane().add(lblPortNumber);
		
		comboConnection = new JComboBox();
		comboConnection.setFont(new Font("Tahoma", Font.PLAIN, 22));
		comboConnection.setBounds(106, 95, 104, 40);
		comboConnection.addItem(1);
		comboConnection.addItem(2);
		comboConnection.addItem(3);
		comboConnection.addItem(4);
		comboConnection.addItem(5);
		comboConnection.addItem(6);
		comboConnection.addItem(7);
		comboConnection.addItem(8);
		frame.getContentPane().add(comboConnection);
		
		button = new JButton("Stream");
		button.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{

				if (validPort())
					controller.startStop();
				
				else
					JOptionPane.showMessageDialog(frame, "Port nummer fejler");
			}

			
		});
		button.setBounds(7, 146, 203, 78);
		frame.getContentPane().add(button);
		
		JLabel lblNumberOgConnections = new JLabel("Connections");
		lblNumberOgConnections.setBounds(106, 77, 86, 14);
		frame.getContentPane().add(lblNumberOgConnections);
		
		progressBar = new JProgressBar();
		progressBar.setToolTipText("");
		progressBar.setBounds(10, 235, 200, 33);
		frame.getContentPane().add(progressBar);
	}

	public void updateProgressbar() 
	{
		if (progressBar.getValue() < 100)
			progressBar.setValue(progressBar.getValue() + 1);
		
		else
			progressBar.setValue(0);
	}
	
	
	
	private boolean validPort()
	{
		try
		{
			int check = Integer.parseInt(textPort.getText());
			return true;
		}
		
		catch(Exception e)
		{
			return false;
		}
	}
	
	public void toggleButtonText() 
	{
		if (button.getText().equals("Stream"))
			button.setText("Stop");
		
		else
			button.setText("Stream");
	}
	
	private int parsePort()
	{
		return Integer.parseInt(textPort.getText());
	}

	public String getServerIP() 
	{
		return textIp.getText();
	}
	
	public int getConnections()
	{
		return Integer.parseInt(comboConnection.getSelectedItem().toString());
	}

	public int getServerPort() 
	{
		return parsePort();
	}
	
	public void displayDialog(String message)
	{
		JOptionPane.showMessageDialog(frame, message);
	}

}
